/*
 * highlighter.c
 * This file is part of gURL
 *
 * Copyright (C) 2014 - Aleksey Konovalov
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Library General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "highlighter.h"

#include <string.h>

enum MimeType {
    mtJSON,
    mtSGML,
    mtUNKNOWN
};

static enum MimeType determine_mime_type(const gchar* mimetype) {
    char* semicolon = strchr(mimetype, ';');
    size_t mimelength = semicolon ? semicolon - mimetype - 1 : strlen(mimetype);
    if(!strncmp(mimetype, "application/json", mimelength))
        return mtJSON;
    if(!strncmp(mimetype, "application/sgml", mimelength) ||
       !strncmp(mimetype, "text/sgml", mimelength) ||
       !strncmp(mimetype, "text/html", mimelength) ||
       !strncmp(mimetype, "application/xhtml+xml", mimelength) ||
       !strncmp(mimetype, "application/xml", mimelength) ||
       !strncmp(mimetype, "text/xml", mimelength))
        return mtSGML;
    return mtUNKNOWN;
}

gboolean highlight(GtkTextBuffer* tb, gchar* text, size_t len, gchar* mimetype,
                   gboolean prettyprint) {
    enum MimeType mt = determine_mime_type(mimetype);
    switch(mt) {
        case mtJSON:
            highlight_json(tb, text, len, prettyprint);
            return TRUE;
        case mtSGML:
            highlight_sgml(tb, text, len, prettyprint);
            return TRUE;
        default:
            return FALSE;
    }
}

gboolean prettyprint_available(gchar* mimetype) {
    enum MimeType mt = determine_mime_type(mimetype);
    return mt == mtJSON;
}
